package log

import (
	"github.com/gin-gonic/gin"
	"github.com/magicianlyx/GoLog"
	"github.com/magicianlyx/GoLog/graph"
	"github.com/magicianlyx/GoLog/location"
	"time"
)

func GinLogger() gin.HandlerFunc {
	return func(c *gin.Context) {
		msg := GoLog.NewGinMsgByContext(c, serviceName)
		log.Log(msg)
	}
}

func InfoLog(info string, v map[string]interface{}) {
	invoker := location.CreateLocation(1)
	InfoLogWithLocation(invoker, info, v)
}

func WarnLog(typ *graph.Error, params map[string]interface{}, err error) error {
	invoker := location.CreateLocationChainReverse(1)
	return log.WarnLog(typ, invoker, params, err)
}

func ErrorLog(typ *graph.Error, params map[string]interface{}, err error) error {
	invoker := location.CreateLocationChainReverse(1)
	return ErrorLogWithLocation(typ, invoker, params, err)
}

func FatalLog(typ *graph.Error, params map[string]interface{}, err error) error {
	invoker := location.CreateLocationChainReverse(1)
	return FatalLogWithLocation(typ, invoker, params, err)
}

func InfoLogWithLocation(invoker *location.Location, info string, params map[string]interface{}) {
	log.InfoLog([]*location.Location{invoker}, info, params)
}

func ErrorLogWithLocation(typ graph.IError, invoker []*location.Location, params map[string]interface{}, err error) error {
	return log.ErrorLog(typ, invoker, params, err)
}

func FatalLogWithLocation(typ graph.IError, invoker []*location.Location, params map[string]interface{}, err error) error {
	return log.FatalLog(typ, invoker, params, err)
}

// 封装的计时器
func Timer() func() time.Duration {
	return GoLog.Timer()
}

func CreateLocation(skip int) *location.Location {
	return location.CreateLocation(skip + 1)
}

func CreateLocationChainReverse(skip int) []*location.Location {
	return location.CreateLocationChainReverse(skip + 1)
}
